//
//  HNSH_NoticeViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/28.
//

#import "HNSH_NoticeViewController.h"

//view
#import "HNSH_NoticeCell.h"

//vc
#import "HNSH_NoticeDetailViewController.h"

@interface HNSH_NoticeViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

@end

static NSString *HNSH_NoticeCellID = @"HNSH_NoticeCellID";

@implementation HNSH_NoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"官方公告" showLeftItem:YES];
    
    [self configTableView];
}

- (void)configTableView {
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
    
    [self.tableView registerClass:[HNSH_NoticeCell class] forCellReuseIdentifier:HNSH_NoticeCellID];
}

#pragma mark networking
- (void)refreshData:(NSInteger)page {
    [HNSH_UserNetServer requestNoticeMessageListSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSArray * list = responseObject.resultset[@"list"];
            NSArray * array = [NSArray yy_modelArrayWithClass:HNSH_NoticeModel.class json:list];
            self.listAry = array;
            [self endRefresh:array.count>=10];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadNoticeMessageClearReadFlag {
    [HNSH_UserNetServer requestNoticeMessageClearReadFlagSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"清除成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kClearNotice" object:nil];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_NoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_NoticeCellID];
    cell.model = self.listAry[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_NoticeDetailViewController *vc = [[HNSH_NoticeDetailViewController alloc]init];
    vc.model = self.listAry[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
